#include "uart_my.h"

static QueueHandle_t uart_queue;

int size = 0;
char msg[RD_BUF_SIZE] = "";
int state = 0;

void Uart1Log()
{
    uart_config_t uart_config = {
        .baud_rate = 74880, //波特率
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO2_U, FUNC_U1TXD_BK);
    uart_param_config(UART_NUM_1, &uart_config);
    uart_driver_install(UART_NUM_1, 0, BUF_SIZE + 1, BUF_SIZE + 1, NULL, 0);
}

void Uart0Transmit()
{
    uart_config_t uart_config = {
        .baud_rate = 74880, //波特率
        .data_bits = UART_DATA_8_BITS,
        .parity = UART_PARITY_DISABLE,
        .stop_bits = UART_STOP_BITS_1,
        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE};

    uart_param_config(uart_log_port, &uart_config);
    uart_driver_install(uart_log_port, BUF_SIZE * 2, BUF_SIZE * 2, 100, &uart_queue, 0);
}

void uart_event_task(void *pvParameters)
{
    uart_event_t event;

    char *p = msg;

    for (;;)
    {
        // Waiting for UART event.
        if (xQueueReceive(uart_queue, (void *)&event, (portTickType)portMAX_DELAY))
        {
            bzero(p, RD_BUF_SIZE);
            // ESP_LOGI(TAG, "uart[%d] event:", UART_NUM_0);

            switch (event.type)
            {
            // Event of UART receving data
            // We'd better handler data event fast, there would be much more data events than
            // other types of events. If we take too much time on data event, the queue might be full.
            case UART_DATA:
                // ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
                uart_read_bytes(uart_log_port, (uint8_t *)p, event.size, portMAX_DELAY);
                // ESP_LOGI(TAG, "[DATA EVT]:");
                size = event.size;
                // uart_write_bytes(UART_NUM_0, (const char *)msg, size);
                state = 1;

                break;

            // Event of HW FIFO overflow detected
            case UART_FIFO_OVF:
                // ESP_LOGI(TAG, "hw fifo overflow");
                // If fifo overflow happened, you should consider adding flow control for your application.
                // The ISR has already reset the rx FIFO,
                // As an example, we directly flush the rx buffer here in order to read more data.
                uart_flush_input(UART_NUM_0);
                xQueueReset(uart_queue);
                break;

            // Event of UART ring buffer full
            case UART_BUFFER_FULL:
                // ESP_LOGI(TAG, "ring buffer full");
                // If buffer full happened, you should consider encreasing your buffer size
                // As an example, we directly flush the rx buffer here in order to read more data.
                uart_flush_input(UART_NUM_0);
                xQueueReset(uart_queue);
                break;

            case UART_PARITY_ERR:
                // ESP_LOGI(TAG, "uart parity error");
                break;

            // Event of UART frame error
            case UART_FRAME_ERR:
                // ESP_LOGI(TAG, "uart frame error");
                break;

            // Others
            default:
                // ESP_LOGI(TAG, "uart event type: %d", event.type);
                break;
            }
        }
    }

    // dtmp = NULL;
    vTaskDelete(NULL);
}
